<!-- eslint-disable vue/valid-v-for -->
<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="4">
              <a-form-item label="级别" prop="level">
                <a-select
                  placeholder="请选择级别"
                  v-model="queryParam.level"
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="(d, index) in levelOptions"
                    :key="index"
                    :value="d.dictValue"
                    >{{ d.dictLabel }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="联系方式" prop="ideasType">
                <a-select
                  placeholder="请选择联系方式"
                  v-model="queryParam.ideasType"
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="(d, index) in ideasTypeOptions"
                    :key="index"
                    :value="d.dictValue"
                    >{{ d.dictLabel }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-customer-shadow-info-add-form
        v-if="showAddModal"
        ref="bizCustomerShadowInfoAddForm"
        :levelOptions="levelOptions"
        :ideasTypeOptions="ideasTypeOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-customer-shadow-info-edit-form
        v-if="showEditModal"
        ref="bizCustomerShadowInfoEditForm"
        :levelOptions="levelOptions"
        :ideasTypeOptions="ideasTypeOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="客户跟踪记录"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizCustomerShadowInfo-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizCustomerShadowInfoList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <div class="table-operations" slot="button">
          <a-button type="" @click="handleExport">
            <a-icon type="download" />导出
          </a-button>
        </div>

        <span slot="level" slot-scope="{ record }">
          {{ levelFormat(record) }}
        </span>

        <span slot="ideasType" slot-scope="{ record }">
          {{ ideasTypeFormat(record) }}
        </span>
        <span slot="images" slot-scope="{ record }">
          <span>{{ record.takes }}</span
          ><br />
          <span v-if="record.images">
            <template v-for="item in record.images.split(';')">
              <a-avatar
                shape="square"
                :src="item"
                style="
                  width: 45px;
                  height: 55px;
                  border: 1px solid #cccccc;
                  margin-right: 20px;
                "
                @click="showModal(item)"
              />
             
            </template>
          </span>
        </span>
        <span slot="mark" slot-scope="{ text }">
          <a-tag v-if="text === 1" color="#87d068">标记</a-tag>
          <a-tag v-else>未标记</a-tag>
        </span>
        <span slot="operation" slot-scope="{ text, record }">
          <a
            @click="handleUpdate(record)"
            v-hasPermi="['biz:bizCustomerShadowInfo:edit']"
          >
            修改
          </a>
          <a-divider
            type="vertical"
            v-hasPermi="['biz:bizCustomerShadowInfo:remove']"
          />
          <a
            @click="handleDelete(record)"
            v-hasPermi="['biz:bizCustomerShadowInfo:remove']"
          >
            删除
          </a>
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
      </advance-table>
    </a-card>
    <a-modal
      title="查看图片"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      v-if="visible"
    >
      <a-row>
        <a-col :span="24" style="text-align: center"
          ><img :src="imgsrc" width="70%" height="70%"
        /></a-col>
      </a-row>
    </a-modal>
  </div>
</template>
<script>
import {
  listBizCustomerShadowInfo,
  delBizCustomerShadowInfo,
  exportBizCustomerShadowInfo,
} from "@/api/biz/bizCustomerShadowInfo";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizCustomerShadowInfoAddForm from "@/views/biz/customershadowinfo/modules/BizCustomerShadowInfoAddForm";
import BizCustomerShadowInfoEditForm from "@/views/biz/customershadowinfo/modules/BizCustomerShadowInfoEditForm";
export default {
  name: "BizCustomerShadowInfo",
  components: {
    AdvanceTable,
    BizCustomerShadowInfoAddForm,
    BizCustomerShadowInfoEditForm,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      visible: false,
      imgsrc: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 客户跟踪表格数据
      bizCustomerShadowInfoList: [],
      // 重要级别字典
      levelOptions: [],
      // 联系方式字典
      ideasTypeOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        level: undefined,

        ideasType: undefined,
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "3%",
          scopedSlots: { customRender: "rowindex" },
        },
        {
          title: "客户姓名",
          dataIndex: "name",
          ellipsis: true,
          align: "left",
          width: "8%",
        },
        {
          title: "联系电话",
          dataIndex: "phone",
          ellipsis: true,
          align: "left",
          width: "8%",
        },
        {
          title: "沟通方式",
          dataIndex: "ideasType",
          scopedSlots: { customRender: "ideasType" },
          align: "center",
          width: "7%",
        },
        {
          title: "跟进时间",
          dataIndex: "createTime",
          align: "center",
          width: "12%",
        },
        {
          title: "下次跟进时间",
          dataIndex: "nextTime",
          align: "center",
          width: "10%",
        },
        {
          title: "级别",
          dataIndex: "level",
          scopedSlots: { customRender: "level" },
          align: "center",
          width: "5%",
        },
        {
          title: "重点标记",
          dataIndex: "mark",
          align: "center",
          scopedSlots: { customRender: "mark" },
          width: "6%",
        },
        {
          title: "跟进内容",
          dataIndex: "takes",
          ellipsis: true,
          align: "left",
          width: "45%",
          scopedSlots: { customRender: "images" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("customer_lianxi_level").then((response) => {
      this.levelOptions = response.data;
    });
    this.getDicts("customer_lianxi_type").then((response) => {
      this.ideasTypeOptions = response.data;
    });
  },
  methods: {
    handleCancel(e) {
      console.log("Clicked cancel button");
      this.visible = false;
    },
    showModal(src) {
      this.visible = true;
      this.imgsrc = src;
    },
    handleOk(e) {
      console.log(e);
      this.visible = false;
    },
    /** 查询客户跟踪列表 */
    getList() {
      this.loading = true;
      listBizCustomerShadowInfo(this.queryParam).then((response) => {
        this.bizCustomerShadowInfoList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 重要级别字典翻译
    levelFormat(row) {
      if (row.level) {
        return this.selectDictLabel(this.levelOptions, row.level);
      } else {
        return "";
      }
    },
    // 联系方式字典翻译
    ideasTypeFormat(row) {
      if (row.ideasType) {
        return this.selectDictLabel(this.ideasTypeOptions, row.ideasType);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        level: undefined,

        ideasType: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizCustomerShadowInfoAddForm.handleAdd());
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizCustomerShadowInfoEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizCustomerShadowInfoIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizCustomerShadowInfo(bizCustomerShadowInfoIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizCustomerShadowInfo(that.queryParam).then(
            (response) => {
              that.download(response.msg);
              that.$message.success("导出成功", 3);
            }
          );
        },
        onCancel() {},
      });
    },
  },
};
</script>
